<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>

	
		<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
		<meta http-equiv="Content-Language" content="en-us">

		<title>Django | Models | Django Documentation</title>

		<meta name="ROBOTS" content="ALL">
		<meta http-equiv="imagetoolbar" content="no">
		<meta name="MSSmartTagsPreventParsing" content="true">
		<meta name="Copyright" content="This site's design and contents Copyright (c) 2005  Lawrence Journal-World.">

		<meta name="keywords" content="Python, Django, framework, open-source">
		<meta name="description" content="Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.">

		<link href="Django%20%7C%20Models%20%7C%20Django%20Documentation_files/base.css" rel="stylesheet" type="text/css" media="screen">
		<link href="Django%20%7C%20Models%20%7C%20Django%20Documentation_files/print.css" rel="stylesheet" type="text/css" media="print">
      
  
  <style type="text/css" media="screen">
    #docs-search {
      color: #000;
      float: right;
    }
    #docs-search form {
      font-size: 92%;
      margin: 0;
      padding: 1em 1em 0;
      white-space: nowrap;
    }
    form.search ul {
      list-style: none;
      margin: 0;
      padding: 0;
    }
    form.search li {
      display: inline;
      padding-right: 1em;
    }
    form.search .query {
      width: 18em;
    }
  </style>
  <link rel="stylesheet" href="Django%20%7C%20Models%20%7C%20Django%20Documentation_files/pygments.css" type="text/css">

	</head><body id="documentation" class="default">

	<div id="container">
		<div id="header">
			<h1 id="logo"><a href="http://www.djangoproject.com/"><img src="Django%20%7C%20Models%20%7C%20Django%20Documentation_files/hdr_logo.gif" alt="Django"></a></h1>
			<ul id="nav-global">
				<li id="nav-homepage"><a href="http://www.djangoproject.com/">Home</a></li>
				<li id="nav-download"><a href="http://www.djangoproject.com/download/">Download</a></li>
				<li id="nav-documentation"><a href="http://docs.djangoproject.com/">Documentation</a></li>
				<li id="nav-weblog"><a href="http://www.djangoproject.com/weblog/">Weblog</a></li>
				<li id="nav-community"><a href="http://www.djangoproject.com/community/">Community</a></li>
				<li id="nav-code"><a href="http://code.djangoproject.com/">Code</a></li>
			</ul>
		</div>
		<!-- END Header -->
		<div id="billboard">
  <h2><a href="http://docs.djangoproject.com/en/1.0/">Django documentation</a></h2>
</div>
		<div id="columnwrap">
			
		<div id="content-main">
		


  <h2 class="deck">
  
    This document describes Django version 1.0. For development documentation, 
    <a href="http://docs.djangoproject.com/en/dev/topics/db/models/">go here</a>.
  
  </h2>
  <div class="section" id="s-module-django.db.models">
<span id="s-topics-db-models"></span><span id="module-django.db.models"></span><span id="topics-db-models"></span><h1>Models<a class="headerlink" href="#module-django.db.models" title="Permalink to this headline">¶</a></h1>
<p>A model is the single, definitive source of data about your data. It contains
the essential fields and behaviors of the data you’re storing. Generally, each
model maps to a single database table.</p>
<p>The basics:</p>
<ul class="simple">
<li>Each model is a Python class that subclasses
<a title="django.db.models.Model" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/instances/#django.db.models.Model"><tt class="xref docutils literal"><span class="pre">django.db.models.Model</span></tt></a>.</li>
<li>Each attribute of the model represents a database field.</li>
<li>With all of this, Django gives you an automatically-generated
database-access API; see <a class="reference external" href="http://docs.djangoproject.com/en/1.0/topics/db/queries/#topics-db-queries"><em>Making queries</em></a>.</li>
</ul>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">A companion to this document is the <a class="reference external" href="http://www.djangoproject.com/documentation/models/">official repository of model
examples</a>. (In the Django source distribution, these examples are in the
<tt class="docutils literal"><span class="pre">tests/modeltests</span></tt> directory.)</p>
</div>
<div class="section" id="s-quick-example">
<span id="quick-example"></span><h2>Quick example<a class="headerlink" href="#quick-example" title="Permalink to this headline">¶</a></h2>
<p>This example model defines a <tt class="docutils literal"><span class="pre">Person</span></tt>, which has a <tt class="docutils literal"><span class="pre">first_name</span></tt> and
<tt class="docutils literal"><span class="pre">last_name</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">30</span><span class="p">)</span>
    <span class="n">last_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">30</span><span class="p">)</span>
</pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">first_name</span></tt> and <tt class="docutils literal"><span class="pre">last_name</span></tt> are <a class="reference internal" href="#fields">fields</a> of the model. Each field is
specified as a class attribute, and each attribute maps to a database column.</p>
<p>The above <tt class="docutils literal"><span class="pre">Person</span></tt> model would create a database table like this:</p>
<div class="highlight-sql"><div class="highlight"><pre><span class="k">CREATE</span> <span class="k">TABLE</span> <span class="n">myapp_person</span> <span class="p">(</span>
    <span class="ss">"id"</span> <span class="nb">serial</span> <span class="k">NOT</span> <span class="k">NULL</span> <span class="k">PRIMARY</span> <span class="k">KEY</span><span class="p">,</span>
    <span class="ss">"first_name"</span> <span class="nb">varchar</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
    <span class="ss">"last_name"</span> <span class="nb">varchar</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">NULL</span>
<span class="p">);</span>
</pre></div>
</div>
<p>Some technical notes:</p>
<ul class="simple">
<li>The name of the table, <tt class="docutils literal"><span class="pre">myapp_person</span></tt>, is automatically derived from
some model metadata but can be overridden. See <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/options/#table-names"><em>Table names</em></a> for more
details..</li>
<li>An <tt class="docutils literal"><span class="pre">id</span></tt> field is added automatically, but this behavior can be
overridden. See <a class="reference internal" href="#automatic-primary-key-fields"><em>Automatic primary key fields</em></a>.</li>
<li>The <tt class="docutils literal"><span class="pre">CREATE</span> <span class="pre">TABLE</span></tt> SQL in this example is formatted using PostgreSQL
syntax, but it's worth noting Django uses SQL tailored to the database
backend specified in your <a class="reference external" href="http://docs.djangoproject.com/en/1.0/topics/settings/#topics-settings"><em>settings file</em></a>.</li>
</ul>
</div>
<div class="section" id="s-using-models">
<span id="using-models"></span><h2>Using models<a class="headerlink" href="#using-models" title="Permalink to this headline">¶</a></h2>
<p>Once you have defined your models, you need to tell Django you're going to <em>use</em>
those models. Do this by editing your settings file and changing the
<a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/settings/#setting-INSTALLED_APPS"><tt class="xref docutils literal"><span class="pre">INSTALLED_APPS</span></tt></a> setting to add the name of the module that contains
your <tt class="docutils literal"><span class="pre">models.py</span></tt>.</p>
<p>For example, if the models for your application live in the module
<tt class="docutils literal"><span class="pre">mysite.myapp.models</span></tt> (the package structure that is created for an
application by the <tt class="xref docutils literal"><span class="pre">manage.py</span> <span class="pre">startapp</span></tt> script),
<a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/settings/#setting-INSTALLED_APPS"><tt class="xref docutils literal"><span class="pre">INSTALLED_APPS</span></tt></a> should read, in part:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">INSTALLED_APPS</span> <span class="o">=</span> <span class="p">(</span>
    <span class="c">#...</span>
    <span class="s">'mysite.myapp'</span><span class="p">,</span>
    <span class="c">#...</span>
<span class="p">)</span>
</pre></div>
</div>
<p>When you add new apps to <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/settings/#setting-INSTALLED_APPS"><tt class="xref docutils literal"><span class="pre">INSTALLED_APPS</span></tt></a>, be sure to run
<tt class="xref docutils literal"><span class="pre">manage.py</span> <span class="pre">syncdb</span></tt>.</p>
</div>
<div class="section" id="s-fields">
<span id="fields"></span><h2>Fields<a class="headerlink" href="#fields" title="Permalink to this headline">¶</a></h2>
<p>The most important part of a model -- and the only required part of a model --
is the list of database fields it defines. Fields are specified by class
attributes.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Musician</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">50</span><span class="p">)</span>
    <span class="n">last_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">50</span><span class="p">)</span>
    <span class="n">instrument</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">100</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Album</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">artist</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Musician</span><span class="p">)</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">100</span><span class="p">)</span>
    <span class="n">release_date</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">()</span>
    <span class="n">num_stars</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">IntegerField</span><span class="p">()</span>
</pre></div>
</div>
<div class="section" id="s-field-types">
<span id="field-types"></span><h3>Field types<a class="headerlink" href="#field-types" title="Permalink to this headline">¶</a></h3>
<p>Each field in your model should be an instance of the appropriate
<tt class="xref docutils literal"><span class="pre">Field</span></tt> class. Django uses the field class types to
determine a few things:</p>
<ul class="simple">
<li>The database column type (e.g. <tt class="docutils literal"><span class="pre">INTEGER</span></tt>, <tt class="docutils literal"><span class="pre">VARCHAR</span></tt>).</li>
<li>The widget to use in Django's admin interface, if you care to use it
(e.g. <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type="text"&gt;</span></tt>, <tt class="docutils literal"><span class="pre">&lt;select&gt;</span></tt>).</li>
<li>The minimal validation requirements, used in Django's admin and in
automatically-generated forms.</li>
</ul>
<p>Django ships with dozens of built-in field types; you can find the complete list
in the <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#model-field-types"><em>model field reference</em></a>. You can easily write
your own fields if Django's built-in ones don't do the trick; see
<a class="reference external" href="http://docs.djangoproject.com/en/1.0/howto/custom-model-fields/#howto-custom-model-fields"><em>Writing custom model fields</em></a>.</p>
</div>
<div class="section" id="s-field-options">
<span id="field-options"></span><h3>Field options<a class="headerlink" href="#field-options" title="Permalink to this headline">¶</a></h3>
<p>Each field takes a certain set of field-specific arguments (documented in the
<a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#model-field-types"><em>model field reference</em></a>). For example,
<a title="django.db.models.CharField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.CharField"><tt class="xref docutils literal"><span class="pre">CharField</span></tt></a> (and its subclasses) require a
<a title="django.db.models.CharField.max_length" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.CharField.max_length"><tt class="xref docutils literal"><span class="pre">max_length</span></tt></a> argument which specifies the size
of the <tt class="docutils literal"><span class="pre">VARCHAR</span></tt> database field used to store the data.</p>
<p>There's also a set of common arguments available to all field types. All are
optional. They're fully explained in the <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#common-model-field-options"><em>reference</em></a>, but here's a quick summary of the most often-used
ones:</p>
<dl class="docutils">
<dt><a title="django.db.models.Field.null" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.null"><tt class="xref docutils literal"><span class="pre">null</span></tt></a></dt>
<dd>If <tt class="xref docutils literal"><span class="pre">True</span></tt>, Django will store empty values as <tt class="docutils literal"><span class="pre">NULL</span></tt> in the database.
Default is <tt class="xref docutils literal"><span class="pre">False</span></tt>.</dd>
<dt><a title="django.db.models.Field.blank" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.blank"><tt class="xref docutils literal"><span class="pre">blank</span></tt></a></dt>
<dd><p class="first">If <tt class="xref docutils literal"><span class="pre">True</span></tt>, the field is allowed to be blank. Default is <tt class="xref docutils literal"><span class="pre">False</span></tt>.</p>
<p class="last">Note that this is different than <a title="django.db.models.Field.null" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.null"><tt class="xref docutils literal"><span class="pre">null</span></tt></a>.
<a title="django.db.models.Field.null" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.null"><tt class="xref docutils literal"><span class="pre">null</span></tt></a> is purely database-related, whereas
<a title="django.db.models.Field.blank" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.blank"><tt class="xref docutils literal"><span class="pre">blank</span></tt></a> is validation-related. If a field has
<a title="django.db.models.Field.blank" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.blank"><tt class="xref docutils literal"><span class="pre">blank=True</span></tt></a>, validation on Django's admin site will
allow entry of an empty value. If a field has <a title="django.db.models.Field.blank" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.blank"><tt class="xref docutils literal"><span class="pre">blank=False</span></tt></a>, the field will be required.</p>
</dd>
<dt><a title="django.db.models.Field.choices" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.choices"><tt class="xref docutils literal"><span class="pre">choices</span></tt></a></dt>
<dd><p class="first">An iterable (e.g., a list or tuple) of 2-tuples to use as choices for
this field. If this is given, Django's admin will use a select box
instead of the standard text field and will limit choices to the choices
given.</p>
<p>A choices list looks like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">YEAR_IN_SCHOOL_CHOICES</span> <span class="o">=</span> <span class="p">(</span>
    <span class="p">(</span><span class="s">u'FR'</span><span class="p">,</span> <span class="s">u'Freshman'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">u'SO'</span><span class="p">,</span> <span class="s">u'Sophomore'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">u'JR'</span><span class="p">,</span> <span class="s">u'Junior'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">u'SR'</span><span class="p">,</span> <span class="s">u'Senior'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">u'GR'</span><span class="p">,</span> <span class="s">u'Graduate'</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>The first element in each tuple is the value that will be stored in the
database, the second element will be displayed by the admin interface,
or in a ModelChoiceField. Given an instance of a model object, the
display value for a choices field can be accessed using the
<tt class="docutils literal"><span class="pre">get_FOO_display</span></tt> method. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">GENDER_CHOICES</span> <span class="o">=</span> <span class="p">(</span>
        <span class="p">(</span><span class="s">u'M'</span><span class="p">,</span> <span class="s">u'Male'</span><span class="p">),</span>
        <span class="p">(</span><span class="s">u'F'</span><span class="p">,</span> <span class="s">u'Female'</span><span class="p">),</span>
    <span class="p">)</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">60</span><span class="p">)</span>
    <span class="n">gender</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">2</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="n">GENDER_CHOICES</span><span class="p">)</span>
</pre></div>
</div>
<div class="last highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"Fred Flinstone"</span><span class="p">,</span> <span class="n">gender</span><span class="o">=</span><span class="s">"M"</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">gender</span>
<span class="go">u'M'</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">get_gender_display</span><span class="p">()</span>
<span class="go">u'Male'</span>
</pre></div>
</div>
</dd>
<dt><a title="django.db.models.Field.default" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.default"><tt class="xref docutils literal"><span class="pre">default</span></tt></a></dt>
<dd>The default value for the field. This can be a value or a callable
object. If callable it will be called every time a new object is
created.</dd>
<dt><a title="django.db.models.Field.help_text" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.help_text"><tt class="xref docutils literal"><span class="pre">help_text</span></tt></a></dt>
<dd>Extra "help" text to be displayed under the field on the object's admin
form. It's useful for documentation even if your object doesn't have an
admin form.</dd>
<dt><a title="django.db.models.Field.primary_key" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.primary_key"><tt class="xref docutils literal"><span class="pre">primary_key</span></tt></a></dt>
<dd><p class="first">If <tt class="xref docutils literal"><span class="pre">True</span></tt>, this field is the primary key for the model.</p>
<p class="last">If you don't specify <a title="django.db.models.Field.primary_key" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.primary_key"><tt class="xref docutils literal"><span class="pre">primary_key=True</span></tt></a> for
any fields in your model, Django will automatically add an
<a title="django.db.models.IntegerField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.IntegerField"><tt class="xref docutils literal"><span class="pre">IntegerField</span></tt></a> to hold the primary key, so you don't need to set
<a title="django.db.models.Field.primary_key" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.primary_key"><tt class="xref docutils literal"><span class="pre">primary_key=True</span></tt></a> on any of your fields
unless you want to override the default primary-key behavior. For more,
see <a class="reference internal" href="#automatic-primary-key-fields"><em>Automatic primary key fields</em></a>.</p>
</dd>
<dt><a title="django.db.models.Field.unique" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.unique"><tt class="xref docutils literal"><span class="pre">unique</span></tt></a></dt>
<dd>If <tt class="xref docutils literal"><span class="pre">True</span></tt>, this field must be unique throughout the table.</dd>
</dl>
<p>Again, these are just short descriptions of the most common field options. Full
details can be found in the <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#common-model-field-options"><em>common model field option reference</em></a>.</p>
</div>
<div class="section" id="s-id1">
<span id="s-automatic-primary-key-fields"></span><span id="id1"></span><span id="automatic-primary-key-fields"></span><h3>Automatic primary key fields<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
<p>By default, Django gives each model the following field:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nb">id</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">AutoField</span><span class="p">(</span><span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p>This is an auto-incrementing primary key.</p>
<p>If you'd like to specify a custom primary key, just specify
<a title="django.db.models.Field.primary_key" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.primary_key"><tt class="xref docutils literal"><span class="pre">primary_key=True</span></tt></a> on one of your fields. If Django
sees you've explicitly set <a title="django.db.models.Field.primary_key" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.primary_key"><tt class="xref docutils literal"><span class="pre">Field.primary_key</span></tt></a>, it won't add the automatic
<tt class="docutils literal"><span class="pre">id</span></tt> column.</p>
<p>Each model requires exactly one field to have <a title="django.db.models.Field.primary_key" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.primary_key"><tt class="xref docutils literal"><span class="pre">primary_key=True</span></tt></a>.</p>
</div>
<div class="section" id="s-id2">
<span id="s-verbose-field-names"></span><span id="id2"></span><span id="verbose-field-names"></span><h3>Verbose field names<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3>
<p>Each field type, except for <a title="django.db.models.ForeignKey" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ForeignKey"><tt class="xref docutils literal"><span class="pre">ForeignKey</span></tt></a>,
<a title="django.db.models.ManyToManyField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ManyToManyField"><tt class="xref docutils literal"><span class="pre">ManyToManyField</span></tt></a> and
<a title="django.db.models.OneToOneField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.OneToOneField"><tt class="xref docutils literal"><span class="pre">OneToOneField</span></tt></a>, takes an optional first positional
argument -- a verbose name. If the verbose name isn't given, Django will
automatically create it using the field's attribute name, converting underscores
to spaces.</p>
<p>In this example, the verbose name is <tt class="docutils literal"><span class="pre">"Person's</span> <span class="pre">first</span> <span class="pre">name"</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="s">"Person's first name"</span><span class="p">,</span> <span class="n">max_length</span><span class="o">=</span><span class="mf">30</span><span class="p">)</span>
</pre></div>
</div>
<p>In this example, the verbose name is <tt class="docutils literal"><span class="pre">"first</span> <span class="pre">name"</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">30</span><span class="p">)</span>
</pre></div>
</div>
<p><a title="django.db.models.ForeignKey" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ForeignKey"><tt class="xref docutils literal"><span class="pre">ForeignKey</span></tt></a>,
<a title="django.db.models.ManyToManyField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ManyToManyField"><tt class="xref docutils literal"><span class="pre">ManyToManyField</span></tt></a> and
<a title="django.db.models.OneToOneField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.OneToOneField"><tt class="xref docutils literal"><span class="pre">OneToOneField</span></tt></a> require the first argument to be a
model class, so use the <a title="django.db.models.Field.verbose_name" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.verbose_name"><tt class="xref docutils literal"><span class="pre">verbose_name</span></tt></a> keyword argument:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">poll</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Poll</span><span class="p">,</span> <span class="n">verbose_name</span><span class="o">=</span><span class="s">"the related poll"</span><span class="p">)</span>
<span class="n">sites</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">(</span><span class="n">Site</span><span class="p">,</span> <span class="n">verbose_name</span><span class="o">=</span><span class="s">"list of sites"</span><span class="p">)</span>
<span class="n">place</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">OneToOneField</span><span class="p">(</span><span class="n">Place</span><span class="p">,</span> <span class="n">verbose_name</span><span class="o">=</span><span class="s">"related place"</span><span class="p">)</span>
</pre></div>
</div>
<p>The convention is not to capitalize the first letter of the
<a title="django.db.models.Field.verbose_name" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.verbose_name"><tt class="xref docutils literal"><span class="pre">verbose_name</span></tt></a>. Django will automatically capitalize the first
letter where it needs to.</p>
</div>
<div class="section" id="s-relationships">
<span id="relationships"></span><h3>Relationships<a class="headerlink" href="#relationships" title="Permalink to this headline">¶</a></h3>
<p>Clearly, the power of relational databases lies in relating tables to each
other. Django offers ways to define the three most common types of database
relationships: many-to-one, many-to-many and one-to-one.</p>
<div class="section" id="s-many-to-one-relationships">
<span id="many-to-one-relationships"></span><h4>Many-to-one relationships<a class="headerlink" href="#many-to-one-relationships" title="Permalink to this headline">¶</a></h4>
<p>To define a many-to-one relationship, use <a title="django.db.models.ForeignKey" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ForeignKey"><tt class="xref docutils literal"><span class="pre">ForeignKey</span></tt></a>.
You use it just like any other <tt class="xref docutils literal"><span class="pre">Field</span></tt> type: by
including it as a class attribute of your model.</p>
<p><a title="django.db.models.ForeignKey" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ForeignKey"><tt class="xref docutils literal"><span class="pre">ForeignKey</span></tt></a> requires a positional argument: the class
to which the model is related.</p>
<p>For example, if a <tt class="docutils literal"><span class="pre">Car</span></tt> model has a <tt class="docutils literal"><span class="pre">Manufacturer</span></tt> -- that is, a
<tt class="docutils literal"><span class="pre">Manufacturer</span></tt> makes multiple cars but each <tt class="docutils literal"><span class="pre">Car</span></tt> only has one
<tt class="docutils literal"><span class="pre">Manufacturer</span></tt> -- use the following definitions:</p>
<div class="highlight-python"><pre>class Manufacturer(models.Model):
    # ...

class Car(models.Model):
    manufacturer = models.ForeignKey(Manufacturer)
    # ...</pre>
</div>
<p>You can also create <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#recursive-relationships"><em>recursive relationships</em></a> (an
object with a many-to-one relationship to itself) and <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#lazy-relationships"><em>relationships to
models not yet defined</em></a>; see <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#ref-foreignkey"><em>the model field
reference</em></a> for details.</p>
<p>It's suggested, but not required, that the name of a
<a title="django.db.models.ForeignKey" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ForeignKey"><tt class="xref docutils literal"><span class="pre">ForeignKey</span></tt></a> field (<tt class="docutils literal"><span class="pre">manufacturer</span></tt> in the example
above) be the name of the model, lowercase. You can, of course, call the field
whatever you want. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Car</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">company_that_makes_it</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Manufacturer</span><span class="p">)</span>
    <span class="c"># ...</span>
</pre></div>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">See the <a class="reference external" href="http://www.djangoproject.com/documentation/models/many_to_one/">Many-to-one relationship model example</a> for a full example.</p>
</div>
<p><a title="django.db.models.ForeignKey" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ForeignKey"><tt class="xref docutils literal"><span class="pre">ForeignKey</span></tt></a> fields also accept a number of extra
arguments which are explained in <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#foreign-key-arguments"><em>the model field reference</em></a>. These options help define how the relationship should
work; all are optional.</p>
</div>
<div class="section" id="s-many-to-many-relationships">
<span id="many-to-many-relationships"></span><h4>Many-to-many relationships<a class="headerlink" href="#many-to-many-relationships" title="Permalink to this headline">¶</a></h4>
<p>To define a many-to-many relationship, use
<a title="django.db.models.ManyToManyField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ManyToManyField"><tt class="xref docutils literal"><span class="pre">ManyToManyField</span></tt></a>. You use it just like any other
<tt class="xref docutils literal"><span class="pre">Field</span></tt> type: by including it as a class attribute of
your model.</p>
<p><a title="django.db.models.ManyToManyField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ManyToManyField"><tt class="xref docutils literal"><span class="pre">ManyToManyField</span></tt></a> requires a positional argument: the
class to which the model is related.</p>
<p>For example, if a <tt class="docutils literal"><span class="pre">Pizza</span></tt> has multiple <tt class="docutils literal"><span class="pre">Topping</span></tt> objects -- that is, a
<tt class="docutils literal"><span class="pre">Topping</span></tt> can be on multiple pizzas and each <tt class="docutils literal"><span class="pre">Pizza</span></tt> has multiple toppings
-- here's how you'd represent that:</p>
<div class="highlight-python"><pre>class Topping(models.Model):
    # ...

class Pizza(models.Model):
    # ...
    toppings = models.ManyToManyField(Topping)</pre>
</div>
<p>As with <a title="django.db.models.ForeignKey" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ForeignKey"><tt class="xref docutils literal"><span class="pre">ForeignKey</span></tt></a>, you can also create
<a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#recursive-relationships"><em>recursive relationships</em></a> (an object with a
many-to-one relationship to itself) and <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#lazy-relationships"><em>relationships to models not yet
defined</em></a>; see <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#ref-manytomany"><em>the model field reference</em></a> for details.</p>
<p>It's suggested, but not required, that the name of a
<a title="django.db.models.ManyToManyField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ManyToManyField"><tt class="xref docutils literal"><span class="pre">ManyToManyField</span></tt></a> (<tt class="docutils literal"><span class="pre">toppings</span></tt> in the example above)
be a plural describing the set of related model objects.</p>
<p>It doesn't matter which model gets the
<a title="django.db.models.ManyToManyField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ManyToManyField"><tt class="xref docutils literal"><span class="pre">ManyToManyField</span></tt></a>, but you only need it in one of the
models -- not in both.</p>
<p>Generally, <a title="django.db.models.ManyToManyField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ManyToManyField"><tt class="xref docutils literal"><span class="pre">ManyToManyField</span></tt></a> instances should go in the
object that's going to be edited in the admin interface, if you're using
Django's admin. In the above example, <tt class="docutils literal"><span class="pre">toppings</span></tt> is in <tt class="docutils literal"><span class="pre">Pizza</span></tt> (rather than
<tt class="docutils literal"><span class="pre">Topping</span></tt> having a <tt class="docutils literal"><span class="pre">pizzas</span></tt> <a title="django.db.models.ManyToManyField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ManyToManyField"><tt class="xref docutils literal"><span class="pre">ManyToManyField</span></tt></a> )
because it's more natural to think about a pizza having toppings than a
topping being on multiple pizzas. The way it's set up above, the <tt class="docutils literal"><span class="pre">Pizza</span></tt> admin
form would let users select the toppings.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">See the <a class="reference external" href="http://www.djangoproject.com/documentation/models/many_to_many/">Many-to-many relationship model example</a> for a full example.</p>
</div>
<p><a title="django.db.models.ManyToManyField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ManyToManyField"><tt class="xref docutils literal"><span class="pre">ManyToManyField</span></tt></a> fields also accept a number of extra
arguments which are explained in <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#manytomany-arguments"><em>the model field reference</em></a>. These options help define how the relationship should
work; all are optional.</p>
</div>
<div class="section" id="s-extra-fields-on-many-to-many-relationships">
<span id="s-intermediary-manytomany"></span><span id="extra-fields-on-many-to-many-relationships"></span><span id="intermediary-manytomany"></span><h4>Extra fields on many-to-many relationships<a class="headerlink" href="#extra-fields-on-many-to-many-relationships" title="Permalink to this headline">¶</a></h4>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>When you're only dealing with simple many-to-many relationships such as
mixing and matching pizzas and toppings, a standard <a title="django.db.models.ManyToManyField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ManyToManyField"><tt class="xref docutils literal"><span class="pre">ManyToManyField</span></tt></a> is all you need. However, sometimes
you may need to associate data with the relationship between two models.</p>
<p>For example, consider the case of an application tracking the musical groups
which musicians belong to. There is a many-to-many relationship between a person
and the groups of which they are a member, so you could use a
<a title="django.db.models.ManyToManyField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ManyToManyField"><tt class="xref docutils literal"><span class="pre">ManyToManyField</span></tt></a> to represent this relationship.
However, there is a lot of detail about the membership that you might want to
collect, such as the date at which the person joined the group.</p>
<p>For these situations, Django allows you to specify the model that will be used
to govern the many-to-many relationship. You can then put extra fields on the
intermediate model. The intermediate model is associated with the
<a title="django.db.models.ManyToManyField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ManyToManyField"><tt class="xref docutils literal"><span class="pre">ManyToManyField</span></tt></a> using the
<a title="django.db.models.ManyToManyField.through" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ManyToManyField.through"><tt class="xref docutils literal"><span class="pre">through</span></tt></a> argument to point to the model
that will act as an intermediary. For our musician example, the code would look
something like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">128</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__unicode__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>

<span class="k">class</span> <span class="nc">Group</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">128</span><span class="p">)</span>
    <span class="n">members</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">through</span><span class="o">=</span><span class="s">'Membership'</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__unicode__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>

<span class="k">class</span> <span class="nc">Membership</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">person</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Person</span><span class="p">)</span>
    <span class="n">group</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Group</span><span class="p">)</span>
    <span class="n">date_joined</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">()</span>
    <span class="n">invite_reason</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">64</span><span class="p">)</span>
</pre></div>
</div>
<p>When you set up the intermediary model, you explicitly specify foreign
keys to the models that are involved in the ManyToMany relation. This
explicit declaration defines how the two models are related.</p>
<p>There are a few restrictions on the intermediate model:</p>
<ul class="simple">
<li>Your intermediate model must contain one - and <em>only</em> one - foreign key
to the target model (this would be <tt class="docutils literal"><span class="pre">Person</span></tt> in our example). If you
have more than one foreign key, a validation error will be raised.</li>
<li>Your intermediate model must contain one - and <em>only</em> one - foreign key
to the source model (this would be <tt class="docutils literal"><span class="pre">Group</span></tt> in our example). If you
have more than one foreign key, a validation error will be raised.</li>
<li>The only exception to this is a model which has a many-to-many
relationship to itself, through an intermediary model. In this
case, two foreign keys to the same model are permitted, but they
will be treated as the two (different) sides of the many-to-many
relation.</li>
<li>When defining a many-to-many relationship from a model to
itself, using an intermediary model, you <em>must</em> use
<a title="django.db.models.ManyToManyField.symmetrical" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ManyToManyField.symmetrical"><tt class="xref docutils literal"><span class="pre">symmetrical=False</span></tt></a> (see
<a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#manytomany-arguments"><em>the model field reference</em></a>).</li>
</ul>
<p>Now that you have set up your <a title="django.db.models.ManyToManyField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ManyToManyField"><tt class="xref docutils literal"><span class="pre">ManyToManyField</span></tt></a> to use
your intermediary model (<tt class="docutils literal"><span class="pre">Membership</span></tt>, in this case), you're ready to start
creating some many-to-many relationships. You do this by creating instances of
the intermediate model:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">ringo</span> <span class="o">=</span> <span class="n">Person</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"Ringo Starr"</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">paul</span> <span class="o">=</span> <span class="n">Person</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"Paul McCartney"</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">beatles</span> <span class="o">=</span> <span class="n">Group</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"The Beatles"</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m1</span> <span class="o">=</span> <span class="n">Membership</span><span class="p">(</span><span class="n">person</span><span class="o">=</span><span class="n">ringo</span><span class="p">,</span> <span class="n">group</span><span class="o">=</span><span class="n">beatles</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">date_joined</span><span class="o">=</span><span class="n">date</span><span class="p">(</span><span class="mf">1962</span><span class="p">,</span> <span class="mf">8</span><span class="p">,</span> <span class="mf">16</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">invite_reason</span><span class="o">=</span> <span class="s">"Needed a new drummer."</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m1</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">beatles</span><span class="o">.</span><span class="n">members</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
<span class="go">[&lt;Person: Ringo Starr&gt;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ringo</span><span class="o">.</span><span class="n">group_set</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
<span class="go">[&lt;Group: The Beatles&gt;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span> <span class="o">=</span> <span class="n">Membership</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">person</span><span class="o">=</span><span class="n">paul</span><span class="p">,</span> <span class="n">group</span><span class="o">=</span><span class="n">beatles</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">date_joined</span><span class="o">=</span><span class="n">date</span><span class="p">(</span><span class="mf">1960</span><span class="p">,</span> <span class="mf">8</span><span class="p">,</span> <span class="mf">1</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">invite_reason</span><span class="o">=</span> <span class="s">"Wanted to form a band."</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">beatles</span><span class="o">.</span><span class="n">members</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
<span class="go">[&lt;Person: Ringo Starr&gt;, &lt;Person: Paul McCartney&gt;]</span>
</pre></div>
</div>
<p>Unlike normal many-to-many fields, you <em>can't</em> use <tt class="docutils literal"><span class="pre">add</span></tt>, <tt class="docutils literal"><span class="pre">create</span></tt>,
or assignment (i.e., <tt class="docutils literal"><span class="pre">beatles.members</span> <span class="pre">=</span> <span class="pre">[...]</span></tt>) to create relationships:</p>
<div class="highlight-python"><pre># THIS WILL NOT WORK
&gt;&gt;&gt; beatles.members.add(john)
# NEITHER WILL THIS
&gt;&gt;&gt; beatles.members.create(name="George Harrison")
# AND NEITHER WILL THIS
&gt;&gt;&gt; beatles.members = [john, paul, ringo, george]</pre>
</div>
<p>Why? You can't just create a relationship between a <tt class="docutils literal"><span class="pre">Person</span></tt> and a <tt class="docutils literal"><span class="pre">Group</span></tt>
- you need to specify all the detail for the relationship required by the
<tt class="docutils literal"><span class="pre">Membership</span></tt> model. The simple <tt class="docutils literal"><span class="pre">add</span></tt>, <tt class="docutils literal"><span class="pre">create</span></tt> and assignment calls
don't provide a way to specify this extra detail. As a result, they are
disabled for many-to-many relationships that use an intermediate model.
The only way to create this type of relationship is to create instances of the
intermediate model.</p>
<p>The <tt class="docutils literal"><span class="pre">remove</span></tt> method is disabled for similar reasons. However, the
<tt class="docutils literal"><span class="pre">clear()</span></tt> method can be used to remove all many-to-many relationships
for an instance:</p>
<div class="highlight-python"><pre># Beatles have broken up
&gt;&gt;&gt; beatles.members.clear()</pre>
</div>
<p>Once you have established the many-to-many relationships by creating instances
of your intermediate model, you can issue queries. Just as with normal
many-to-many relationships, you can query using the attributes of the
many-to-many-related model:</p>
<div class="highlight-python"><pre># Find all the groups with a member whose name starts with 'Paul'
&gt;&gt;&gt; Group.objects.filter(members__name__startswith='Paul')
[&lt;Group: The Beatles&gt;]</pre>
</div>
<p>As you are using an intermediate model, you can also query on its attributes:</p>
<div class="highlight-python"><pre># Find all the members of the Beatles that joined after 1 Jan 1961
&gt;&gt;&gt; Person.objects.filter(
...     group__name='The Beatles',
...     membership__date_joined__gt=date(1961,1,1))
[&lt;Person: Ringo Starr]</pre>
</div>
</div>
<div class="section" id="s-one-to-one-relationships">
<span id="one-to-one-relationships"></span><h4>One-to-one relationships<a class="headerlink" href="#one-to-one-relationships" title="Permalink to this headline">¶</a></h4>
<p>To define a one-to-one relationship, use
<a title="django.db.models.OneToOneField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.OneToOneField"><tt class="xref docutils literal"><span class="pre">OneToOneField</span></tt></a>. You use it just like any other
<tt class="docutils literal"><span class="pre">Field</span></tt> type: by including it as a class attribute of your model.</p>
<p>This is most useful on the primary key of an object when that object "extends"
another object in some way.</p>
<p><a title="django.db.models.OneToOneField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.OneToOneField"><tt class="xref docutils literal"><span class="pre">OneToOneField</span></tt></a> requires a positional argument: the
class to which the model is related.</p>
<p>For example, if you were building a database of "places", you would
build pretty standard stuff such as address, phone number, etc. in the
database. Then, if you wanted to build a database of restaurants on
top of the places, instead of repeating yourself and replicating those
fields in the <tt class="docutils literal"><span class="pre">Restaurant</span></tt> model, you could make <tt class="docutils literal"><span class="pre">Restaurant</span></tt> have
a <a title="django.db.models.OneToOneField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.OneToOneField"><tt class="xref docutils literal"><span class="pre">OneToOneField</span></tt></a> to <tt class="docutils literal"><span class="pre">Place</span></tt> (because a
restaurant "is a" place; in fact, to handle this you'd typically use
<a class="reference internal" href="#model-inheritance"><em>inheritance</em></a>, which involves an implicit
one-to-one relation).</p>
<p>As with <a title="django.db.models.ForeignKey" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ForeignKey"><tt class="xref docutils literal"><span class="pre">ForeignKey</span></tt></a>, a
<a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#recursive-relationships"><em>recursive relationship</em></a>
can be defined and
<a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#lazy-relationships"><em>references to as-yet undefined models</em></a>
can be made; see <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#ref-onetoone"><em>the model field reference</em></a> for details.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">See the <a class="reference external" href="http://www.djangoproject.com/documentation/models/one_to_one/">One-to-one relationship model example</a> for a full example.</p>
</div>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p><a title="django.db.models.OneToOneField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.OneToOneField"><tt class="xref docutils literal"><span class="pre">OneToOneField</span></tt></a> fields also accept one optional argument
described in the <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#ref-onetoone"><em>model field reference</em></a>.</p>
<p><a title="django.db.models.OneToOneField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.OneToOneField"><tt class="xref docutils literal"><span class="pre">OneToOneField</span></tt></a> classes used to automatically become
the primary key on a model. This is no longer true (although you can manually
pass in the <a title="django.db.models.Field.primary_key" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.primary_key"><tt class="xref docutils literal"><span class="pre">primary_key</span></tt></a> argument if you like).
Thus, it's now possible to have multiple fields of type
<a title="django.db.models.OneToOneField" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.OneToOneField"><tt class="xref docutils literal"><span class="pre">OneToOneField</span></tt></a> on a single model.</p>
</div>
</div>
<div class="section" id="s-models-across-files">
<span id="models-across-files"></span><h3>Models across files<a class="headerlink" href="#models-across-files" title="Permalink to this headline">¶</a></h3>
<p>It's perfectly OK to relate a model to one from another app. To do this,
import the related model at the top of the model that holds your model. Then,
just refer to the other model class wherever needed. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">mysite.geography.models</span> <span class="k">import</span> <span class="n">ZipCode</span>

<span class="k">class</span> <span class="nc">Restaurant</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="c"># ...</span>
    <span class="n">zip_code</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">ZipCode</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-field-name-restrictions">
<span id="field-name-restrictions"></span><h3>Field name restrictions<a class="headerlink" href="#field-name-restrictions" title="Permalink to this headline">¶</a></h3>
<p>Django places only two restrictions on model field names:</p>
<ol class="arabic">
<li><p class="first">A field name cannot be a Python reserved word, because that would result
in a Python syntax error. For example:</p>
<div class="highlight-python"><pre>class Example(models.Model):
    pass = models.IntegerField() # 'pass' is a reserved word!</pre>
</div>
</li>
<li><p class="first">A field name cannot contain more than one underscore in a row, due to
the way Django's query lookup syntax works. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Example</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">foo__bar</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">IntegerField</span><span class="p">()</span> <span class="c"># 'foo__bar' has two underscores!</span>
</pre></div>
</div>
</li>
</ol>
<p>These limitations can be worked around, though, because your field name doesn't
necessarily have to match your database column name. See the
<a title="django.db.models.Field.db_column" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.Field.db_column"><tt class="xref docutils literal"><span class="pre">db_column</span></tt></a> option.</p>
<p>SQL reserved words, such as <tt class="docutils literal"><span class="pre">join</span></tt>, <tt class="docutils literal"><span class="pre">where</span></tt> or <tt class="docutils literal"><span class="pre">select</span></tt>, <em>are</em> allowed as
model field names, because Django escapes all database table names and column
names in every underlying SQL query. It uses the quoting syntax of your
particular database engine.</p>
</div>
<div class="section" id="s-custom-field-types">
<span id="custom-field-types"></span><h3>Custom field types<a class="headerlink" href="#custom-field-types" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>If one of the existing model fields cannot be used to fit your purposes, or if
you wish to take advantage of some less common database column types, you can
create your own field class. Full coverage of creating your own fields is
provided in <a class="reference external" href="http://docs.djangoproject.com/en/1.0/howto/custom-model-fields/#howto-custom-model-fields"><em>Writing custom model fields</em></a>.</p>
</div>
</div>
<div class="section" id="s-id3">
<span id="s-meta-options"></span><span id="id3"></span><span id="meta-options"></span><h2>Meta options<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h2>
<p>Give your model metadata by using an inner <tt class="docutils literal"><span class="pre">class</span> <span class="pre">Meta</span></tt>, like so:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Ox</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">horn_length</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">IntegerField</span><span class="p">()</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">ordering</span> <span class="o">=</span> <span class="p">[</span><span class="s">"horn_length"</span><span class="p">]</span>
        <span class="n">verbose_name_plural</span> <span class="o">=</span> <span class="s">"oxen"</span>
</pre></div>
</div>
<p>Model metadata is "anything that's not a field", such as ordering options
(<a title="django.db.models.Options.ordering" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/options/#django.db.models.Options.ordering"><tt class="xref docutils literal"><span class="pre">ordering</span></tt></a>), database table name (<a title="django.db.models.Options.db_table" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/options/#django.db.models.Options.db_table"><tt class="xref docutils literal"><span class="pre">db_table</span></tt></a>), or
human-readable singular and plural names (<a title="django.db.models.Options.verbose_name" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/options/#django.db.models.Options.verbose_name"><tt class="xref docutils literal"><span class="pre">verbose_name</span></tt></a> and
<a title="django.db.models.Options.verbose_name_plural" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/options/#django.db.models.Options.verbose_name_plural"><tt class="xref docutils literal"><span class="pre">verbose_name_plural</span></tt></a>). None are required, and adding <tt class="docutils literal"><span class="pre">class</span>
<span class="pre">Meta</span></tt> to a model is completely optional.</p>
<p>A complete list of all possible <tt class="docutils literal"><span class="pre">Meta</span></tt> options can be found in the <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/options/#ref-models-options"><em>model
option reference</em></a>.</p>
</div>
<div class="section" id="s-id4">
<span id="s-model-methods"></span><span id="id4"></span><span id="model-methods"></span><h2>Model methods<a class="headerlink" href="#id4" title="Permalink to this headline">¶</a></h2>
<p>Define custom methods on a model to add custom "row-level" functionality to your
objects. Whereas <a title="django.db.models.Manager" class="reference external" href="http://docs.djangoproject.com/en/1.0/topics/db/managers/#django.db.models.Manager"><tt class="xref docutils literal"><span class="pre">Manager</span></tt></a> methods are intended to do
"table-wide" things, model methods should act on a particular model instance.</p>
<p>This is a valuable technique for keeping business logic in one place -- the
model.</p>
<p>For example, this model has a few custom methods:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.contrib.localflavor.us.models</span> <span class="k">import</span> <span class="n">USStateField</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">50</span><span class="p">)</span>
    <span class="n">last_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">50</span><span class="p">)</span>
    <span class="n">birth_date</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">()</span>
    <span class="n">address</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">100</span><span class="p">)</span>
    <span class="n">city</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">50</span><span class="p">)</span>
    <span class="n">state</span> <span class="o">=</span> <span class="n">USStateField</span><span class="p">()</span> <span class="c"># Yes, this is America-centric...</span>

    <span class="k">def</span> <span class="nf">baby_boomer_status</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">"Returns the person's baby-boomer status."</span>
        <span class="k">import</span> <span class="nn">datetime</span>
        <span class="k">if</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mf">1945</span><span class="p">,</span> <span class="mf">8</span><span class="p">,</span> <span class="mf">1</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">birth_date</span> <span class="o">&lt;=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mf">1964</span><span class="p">,</span> <span class="mf">12</span><span class="p">,</span> <span class="mf">31</span><span class="p">):</span>
            <span class="k">return</span> <span class="s">"Baby boomer"</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">birth_date</span> <span class="o">&lt;</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mf">1945</span><span class="p">,</span> <span class="mf">8</span><span class="p">,</span> <span class="mf">1</span><span class="p">):</span>
            <span class="k">return</span> <span class="s">"Pre-boomer"</span>
        <span class="k">return</span> <span class="s">"Post-boomer"</span>

    <span class="k">def</span> <span class="nf">is_midwestern</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">"Returns True if this person is from the Midwest."</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">state</span> <span class="ow">in</span> <span class="p">(</span><span class="s">'IL'</span><span class="p">,</span> <span class="s">'WI'</span><span class="p">,</span> <span class="s">'MI'</span><span class="p">,</span> <span class="s">'IN'</span><span class="p">,</span> <span class="s">'OH'</span><span class="p">,</span> <span class="s">'IA'</span><span class="p">,</span> <span class="s">'MO'</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_full_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">"Returns the person's full name."</span>
        <span class="k">return</span> <span class="s">'</span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">first_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">last_name</span><span class="p">)</span>
    <span class="n">full_name</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">_get_full_name</span><span class="p">)</span>
</pre></div>
</div>
<p>The last method in this example is a <a class="reference external" href="http://docs.djangoproject.com/en/1.0/glossary/#term-property"><em>property</em></a>. <a class="reference external" href="http://www.python.org/download/releases/2.2/descrintro/#property">Read more about
properties</a>.</p>
<p>The <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/instances/#ref-models-instances"><em>model instance reference</em></a> has a complete list
of <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/instances/#model-instance-methods"><em>methods automatically given to each model</em></a>.
You can override most of these -- see <a class="reference internal" href="#overriding-predefined-model-methods">overriding predefined model methods</a>,
below -- but there are a couple that you'll almost always want to define:</p>
<dl class="docutils">
<dt><a title="django.db.models.Model.__unicode__" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/instances/#django.db.models.Model.__unicode__"><tt class="xref docutils literal"><span class="pre">__unicode__()</span></tt></a></dt>
<dd><p class="first">A Python "magic method" that returns a unicode "representation" of any
object. This is what Python and Django will use whenever a model
instance needs to be coerced and displayed as a plain string. Most
notably, this happens when you display an object in an interactive
console or in the admin.</p>
<p class="last">You'll always want to define this method; the default isn't very helpful
at all.</p>
</dd>
<dt><a title="django.db.models.Model.get_absolute_url" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/instances/#django.db.models.Model.get_absolute_url"><tt class="xref docutils literal"><span class="pre">get_absolute_url()</span></tt></a></dt>
<dd><p class="first">This tells Django how to calculate the URL for an object. Django uses
this in its admin interface, and any time it needs to figure out a URL
for an object.</p>
<p class="last">Any object that has a URL that uniquely identifies it should define this
method.</p>
</dd>
</dl>
<div class="section" id="s-overriding-predefined-model-methods">
<span id="overriding-predefined-model-methods"></span><h3>Overriding predefined model methods<a class="headerlink" href="#overriding-predefined-model-methods" title="Permalink to this headline">¶</a></h3>
<p>There's another set of <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/instances/#model-instance-methods"><em>model methods</em></a> that
encapsulate a bunch of database behavior that you'll want to customize. In
particular you'll often want to change the way <a title="django.db.models.Model.save" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/instances/#django.db.models.Model.save"><tt class="xref docutils literal"><span class="pre">save()</span></tt></a> and
<a title="django.db.models.Model.delete" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/instances/#django.db.models.Model.delete"><tt class="xref docutils literal"><span class="pre">delete()</span></tt></a> work.</p>
<p>You're free to override these methods (and any other model method) to alter
behavior.</p>
<p>A classic use-case for overriding the built-in methods is if you want something
to happen whenever you save an object. For example (see
<a title="django.db.models.Model.save" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/instances/#django.db.models.Model.save"><tt class="xref docutils literal"><span class="pre">save()</span></tt></a> for documentation of the parameters it accepts):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Blog</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">100</span><span class="p">)</span>
    <span class="n">tagline</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">force_insert</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">force_update</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="n">do_something</span><span class="p">()</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Blog</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">force_insert</span><span class="p">,</span> <span class="n">force_update</span><span class="p">)</span> <span class="c"># Call the "real" save() method.</span>
        <span class="n">do_something_else</span><span class="p">()</span>
</pre></div>
</div>
<p>You can also prevent saving:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Blog</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">100</span><span class="p">)</span>
    <span class="n">tagline</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">force_insert</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">force_update</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s">"Yoko Ono's blog"</span><span class="p">:</span>
            <span class="k">return</span> <span class="c"># Yoko shall never have her own blog!</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="nb">super</span><span class="p">(</span><span class="n">Blog</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">force_insert</span><span class="p">,</span> <span class="n">force_update</span><span class="p">)</span> <span class="c"># Call the "real" save() method.</span>
</pre></div>
</div>
<p>It's important to remember to call the superclass method -- that's that
<tt class="docutils literal"><span class="pre">super(Blog,</span> <span class="pre">self).save()</span></tt> business -- to ensure that the object still gets
saved into the database. If you forget to call the superclass method, the
default behavior won't happen and the database won't get touched.</p>
</div>
<div class="section" id="s-executing-custom-sql">
<span id="executing-custom-sql"></span><h3>Executing custom SQL<a class="headerlink" href="#executing-custom-sql" title="Permalink to this headline">¶</a></h3>
<p>Another common pattern is writing custom SQL statements in model methods and
module-level methods. For more details on using raw SQL, see the documentation
on <a class="reference external" href="http://docs.djangoproject.com/en/1.0/topics/db/sql/#topics-db-sql"><em>using raw SQL</em></a>.</p>
</div>
</div>
<div class="section" id="s-id5">
<span id="s-model-inheritance"></span><span id="id5"></span><span id="model-inheritance"></span><h2>Model inheritance<a class="headerlink" href="#id5" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>Model inheritance in Django works almost identically to the way normal
class inheritance works in Python. The only decision you have to make
is whether you want the parent models to be models in their own right
(with their own database tables), or if the parents are just holders
of common information that will only be visible through the child
models.</p>
<p>Often, you will just want to use the parent class to hold information
that you don't want to have to type out for each child model. This
class isn't going to ever be used in isolation, so
<a class="reference internal" href="#abstract-base-classes"><em>Abstract base classes</em></a> are what you're after. However, if you're
subclassing an existing model (perhaps something from another
application entirely), or want each model to have its own database
table, <a class="reference internal" href="#multi-table-inheritance"><em>Multi-table inheritance</em></a> is the way to go.</p>
<div class="section" id="s-id6">
<span id="s-abstract-base-classes"></span><span id="id6"></span><span id="abstract-base-classes"></span><h3>Abstract base classes<a class="headerlink" href="#id6" title="Permalink to this headline">¶</a></h3>
<p>Abstract base classes are useful when you want to put some common
information into a number of other models. You write your base class
and put <tt class="docutils literal"><span class="pre">abstract=True</span></tt> in the <a class="reference internal" href="#meta-options"><em>Meta</em></a>
class. This model will then not be used to create any database
table. Instead, when it is used as a base class for other models, its
fields will be added to those of the child class. It is an error to
have fields in the abstract base class with the same name as those in
the child (and Django will raise an exception).</p>
<p>An example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">CommonInfo</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">100</span><span class="p">)</span>
    <span class="n">age</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">PositiveIntegerField</span><span class="p">()</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">abstract</span> <span class="o">=</span> <span class="bp">True</span>

<span class="k">class</span> <span class="nc">Student</span><span class="p">(</span><span class="n">CommonInfo</span><span class="p">):</span>
    <span class="n">home_group</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">5</span><span class="p">)</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">Student</span></tt> model will have three fields: <tt class="docutils literal"><span class="pre">name</span></tt>, <tt class="docutils literal"><span class="pre">age</span></tt> and
<tt class="docutils literal"><span class="pre">home_group</span></tt>. The <tt class="docutils literal"><span class="pre">CommonInfo</span></tt> model cannot be used as a normal Django
model, since it is an abstract base class. It does not generate a database
table or have a manager, and cannot be instantiated or saved directly.</p>
<p>For many uses, this type of model inheritance will be exactly what you want.
It provides a way to factor out common information at the Python level, whilst
still only creating one database table per child model at the database level.</p>
<div class="section" id="s-meta-inheritance">
<span id="meta-inheritance"></span><h4><tt class="docutils literal"><span class="pre">Meta</span></tt> inheritance<a class="headerlink" href="#meta-inheritance" title="Permalink to this headline">¶</a></h4>
<p>When an abstract base class is created, Django makes any <a class="reference internal" href="#meta-options"><em>Meta</em></a>
inner class you declared in the base class available as an
attribute. If a child class does not declare its own <a class="reference internal" href="#meta-options"><em>Meta</em></a>
class, it will inherit the parent's <a class="reference internal" href="#meta-options"><em>Meta</em></a>. If the child wants to
extend the parent's <a class="reference internal" href="#meta-options"><em>Meta</em></a> class, it can subclass it. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">CommonInfo</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="o">...</span>
    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">abstract</span> <span class="o">=</span> <span class="bp">True</span>
        <span class="n">ordering</span> <span class="o">=</span> <span class="p">[</span><span class="s">'name'</span><span class="p">]</span>

<span class="k">class</span> <span class="nc">Student</span><span class="p">(</span><span class="n">CommonInfo</span><span class="p">):</span>
    <span class="o">...</span>
    <span class="k">class</span> <span class="nc">Meta</span><span class="p">(</span><span class="n">CommonInfo</span><span class="o">.</span><span class="n">Meta</span><span class="p">):</span>
        <span class="n">db_table</span> <span class="o">=</span> <span class="s">'student_info'</span>
</pre></div>
</div>
<p>Django does make one adjustment to the <a class="reference internal" href="#meta-options"><em>Meta</em></a> class of an abstract base
class: before installing the <a class="reference internal" href="#meta-options"><em>Meta</em></a> attribute, it sets <tt class="docutils literal"><span class="pre">abstract=False</span></tt>.
This means that children of abstract base classes don't automatically become
abstract classes themselves. Of course, you can make an abstract base class
that inherits from another abstract base class. You just need to remember to
explicitly set <tt class="docutils literal"><span class="pre">abstract=True</span></tt> each time.</p>
<p>Some attributes won't make sense to include in the <a class="reference internal" href="#meta-options"><em>Meta</em></a> class of an
abstract base class. For example, including <tt class="docutils literal"><span class="pre">db_table</span></tt> would mean that all
the child classes (the ones that don't specify their own <a class="reference internal" href="#meta-options"><em>Meta</em></a>) would use
the same database table, which is almost certainly not what you want.</p>
</div>
<div class="section" id="s-be-careful-with-related-name">
<span id="s-abstract-related-name"></span><span id="be-careful-with-related-name"></span><span id="abstract-related-name"></span><h4>Be careful with <tt class="docutils literal"><span class="pre">related_name</span></tt><a class="headerlink" href="#be-careful-with-related-name" title="Permalink to this headline">¶</a></h4>
<p>If you are using the <a title="django.db.models.ForeignKey.related_name" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ForeignKey.related_name"><tt class="xref docutils literal"><span class="pre">related_name</span></tt></a> attribute on a <tt class="docutils literal"><span class="pre">ForeignKey</span></tt> or
<tt class="docutils literal"><span class="pre">ManyToManyField</span></tt>, you must always specify a <em>unique</em> reverse name for the
field. This would normally cause a problem in abstract base classes, since the
fields on this class are included into each of the child classes, with exactly
the same values for the attributes (including <a title="django.db.models.ForeignKey.related_name" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ForeignKey.related_name"><tt class="xref docutils literal"><span class="pre">related_name</span></tt></a>) each time.</p>
<p>To work around this problem, when you are using <a title="django.db.models.ForeignKey.related_name" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ForeignKey.related_name"><tt class="xref docutils literal"><span class="pre">related_name</span></tt></a> in an
abstract base class (only), part of the name should be the string
<tt class="docutils literal"><span class="pre">'%(class)s'</span></tt>. This is replaced by the lower-cased name of the child class
that the field is used in. Since each class has a different name, each related
name will end up being different. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Base</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">m2m</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">(</span><span class="n">OtherModel</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s">"</span><span class="si">%(class)s</span><span class="s">_related"</span><span class="p">)</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">abstract</span> <span class="o">=</span> <span class="bp">True</span>

<span class="k">class</span> <span class="nc">ChildA</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="k">pass</span>

<span class="k">class</span> <span class="nc">ChildB</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="k">pass</span>
</pre></div>
</div>
<p>The reverse name of the <tt class="docutils literal"><span class="pre">ChildA.m2m</span></tt> field will be <tt class="docutils literal"><span class="pre">childa_related</span></tt>,
whilst the reverse name of the <tt class="docutils literal"><span class="pre">ChildB.m2m</span></tt> field will be
<tt class="docutils literal"><span class="pre">childb_related</span></tt>. It is up to you how you use the <tt class="docutils literal"><span class="pre">'%(class)s'</span></tt> portion to
construct your related name, but if you forget to use it, Django will raise
errors when you validate your models (or run <tt class="xref docutils literal"><span class="pre">syncdb</span></tt>).</p>
<p>If you don't specify a <a title="django.db.models.ForeignKey.related_name" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ForeignKey.related_name"><tt class="xref docutils literal"><span class="pre">related_name</span></tt></a> attribute for a field in an
abstract base class, the default reverse name will be the name of the
child class followed by <tt class="docutils literal"><span class="pre">'_set'</span></tt>, just as it normally would be if
you'd declared the field directly on the child class. For example, in
the above code, if the <a title="django.db.models.ForeignKey.related_name" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ForeignKey.related_name"><tt class="xref docutils literal"><span class="pre">related_name</span></tt></a> attribute was omitted, the
reverse name for the <tt class="docutils literal"><span class="pre">m2m</span></tt> field would be <tt class="docutils literal"><span class="pre">childa_set</span></tt> in the
<tt class="docutils literal"><span class="pre">ChildA</span></tt> case and <tt class="docutils literal"><span class="pre">childb_set</span></tt> for the <tt class="docutils literal"><span class="pre">ChildB</span></tt> field.</p>
</div>
</div>
<div class="section" id="s-id7">
<span id="s-multi-table-inheritance"></span><span id="id7"></span><span id="multi-table-inheritance"></span><h3>Multi-table inheritance<a class="headerlink" href="#id7" title="Permalink to this headline">¶</a></h3>
<p>The second type of model inheritance supported by Django is when each model in
the hierarchy is a model all by itself. Each model corresponds to its own
database table and can be queried and created individually. The inheritance
relationship introduces links between the child model and each of its parents
(via an automatically-created <tt class="xref docutils literal"><span class="pre">OneToOneField</span></tt>).
For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Place</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">50</span><span class="p">)</span>
    <span class="n">address</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mf">80</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Restaurant</span><span class="p">(</span><span class="n">Place</span><span class="p">):</span>
    <span class="n">serves_hot_dogs</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">BooleanField</span><span class="p">()</span>
    <span class="n">serves_pizza</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">BooleanField</span><span class="p">()</span>
</pre></div>
</div>
<p>All of the fields of <tt class="docutils literal"><span class="pre">Place</span></tt> will also be available in <tt class="docutils literal"><span class="pre">Restaurant</span></tt>,
although the data will reside in a different database table. So these are both
possible:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Place</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"Bob's Cafe"</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Restaurant</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"Bob's Cafe"</span><span class="p">)</span>
</pre></div>
</div>
<p>If you have a <tt class="docutils literal"><span class="pre">Place</span></tt> that is also a <tt class="docutils literal"><span class="pre">Restaurant</span></tt>, you can get from the
<tt class="docutils literal"><span class="pre">Place</span></tt> object to the <tt class="docutils literal"><span class="pre">Restaurant</span></tt> object by using the lower-case version
of the model name:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Place</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"Bob's Cafe"</span><span class="p">)</span>
<span class="go"># If Bob's Cafe is a Restaurant object, this will give the child class:</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">restaurant</span>
<span class="go">&lt;Restaurant: ...&gt;</span>
</pre></div>
</div>
<p>However, if <tt class="docutils literal"><span class="pre">p</span></tt> in the above example was <em>not</em> a <tt class="docutils literal"><span class="pre">Restaurant</span></tt> (it had been
created directly as a <tt class="docutils literal"><span class="pre">Place</span></tt> object or was the parent of some other class),
referring to <tt class="docutils literal"><span class="pre">p.restaurant</span></tt> would raise a Restaurant.DoesNotExist exception.</p>
<div class="section" id="s-meta-and-multi-table-inheritance">
<span id="meta-and-multi-table-inheritance"></span><h4><tt class="docutils literal"><span class="pre">Meta</span></tt> and multi-table inheritance<a class="headerlink" href="#meta-and-multi-table-inheritance" title="Permalink to this headline">¶</a></h4>
<p>In the multi-table inheritance situation, it doesn't make sense for a child
class to inherit from its parent's <a class="reference internal" href="#meta-options"><em>Meta</em></a> class. All the <a class="reference internal" href="#meta-options"><em>Meta</em></a> options
have already been applied to the parent class and applying them again would
normally only lead to contradictory behaviour (this is in contrast with the
abstract base class case, where the base class doesn't exist in its own
right).</p>
<p>So a child model does not have access to its parent's <a class="reference internal" href="#meta-options"><em>Meta</em></a> class. However,
there are a few limited cases where the child inherits behaviour from the
parent: if the child does not specify an <a title="django.db.models.Options.ordering" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/options/#django.db.models.Options.ordering"><tt class="xref docutils literal"><span class="pre">django.db.models.Options.ordering</span></tt></a> attribute or a
<a title="django.db.models.Options.get_latest_by" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/options/#django.db.models.Options.get_latest_by"><tt class="xref docutils literal"><span class="pre">django.db.models.Options.get_latest_by</span></tt></a> attribute, it will inherit these from its parent.</p>
<p>If the parent has an ordering and you don't want the child to have any natural
ordering, you can explicitly disable it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">ChildModel</span><span class="p">(</span><span class="n">ParentModel</span><span class="p">):</span>
    <span class="o">...</span>
    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="c"># Remove parent's ordering effect</span>
        <span class="n">ordering</span> <span class="o">=</span> <span class="p">[]</span>
</pre></div>
</div>
</div>
<div class="section" id="s-inheritance-and-reverse-relations">
<span id="inheritance-and-reverse-relations"></span><h4>Inheritance and reverse relations<a class="headerlink" href="#inheritance-and-reverse-relations" title="Permalink to this headline">¶</a></h4>
<p>Because multi-table inheritance uses an implicit
<tt class="xref docutils literal"><span class="pre">OneToOneField</span></tt> to link the child and
the parent, it's possible to move from the parent down to the child,
as in the above example. However, this uses up the name that is the
default <a title="django.db.models.ForeignKey.related_name" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ForeignKey.related_name"><tt class="xref docutils literal"><span class="pre">related_name</span></tt></a> value for
<tt class="xref docutils literal"><span class="pre">django.db.models.fields.ForeignKey</span></tt> and
<tt class="xref docutils literal"><span class="pre">django.db.models.fields.ManyToManyField</span></tt> relations.  If you
are putting those types of relations on a subclass of another model,
you <strong>must</strong> specify the
<a title="django.db.models.ForeignKey.related_name" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/fields/#django.db.models.ForeignKey.related_name"><tt class="xref docutils literal"><span class="pre">related_name</span></tt></a> attribute on each
such field. If you forget, Django will raise an error when you run
<tt class="xref docutils literal"><span class="pre">validate</span></tt> or <tt class="xref docutils literal"><span class="pre">syncdb</span></tt>.</p>
<p>For example, using the above <tt class="docutils literal"><span class="pre">Place</span></tt> class again, let's create another
subclass with a <tt class="xref docutils literal"><span class="pre">ManyToManyField</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Supplier</span><span class="p">(</span><span class="n">Place</span><span class="p">):</span>
    <span class="c"># Must specify related_name on all relations.</span>
    <span class="n">customers</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">(</span><span class="n">Restaurant</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s">'provider'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-specifying-the-parent-link-field">
<span id="specifying-the-parent-link-field"></span><h4>Specifying the parent link field<a class="headerlink" href="#specifying-the-parent-link-field" title="Permalink to this headline">¶</a></h4>
<p>As mentioned, Django will automatically create a
<tt class="xref docutils literal"><span class="pre">OneToOneField</span></tt> linking your child
class back any non-abstract parent models. If you want to control the
name of the attribute linking back to the parent, you can create your
own <tt class="xref docutils literal"><span class="pre">OneToOneField</span></tt> and set
<tt class="xref docutils literal"><span class="pre">parent_link=True</span></tt>
to indicate that your field is the link back to the parent class.</p>
</div>
</div>
<div class="section" id="s-multiple-inheritance">
<span id="multiple-inheritance"></span><h3>Multiple inheritance<a class="headerlink" href="#multiple-inheritance" title="Permalink to this headline">¶</a></h3>
<p>Just as with Python's subclassing, it's possible for a Django model to inherit
from multiple parent models. Keep in mind that normal Python name resolution
rules apply. The first base class that a particular name (e.g. <a class="reference internal" href="#meta-options"><em>Meta</em></a>) appears in will be the one that is used; for example, this
means that if multiple parents contain a <a class="reference internal" href="#meta-options"><em>Meta</em></a> class,
only the first one is going to be used, and all others will be ignored.</p>
<p>Generally, you won't need to inherit from multiple parents. The main use-case
where this is useful is for "mix-in" classes: adding a particular extra
field or method to every class that inherits the mix-in. Try to keep your
inheritance hierarchies as simple and straightforward as possible so that you
won't have to struggle to work out where a particular piece of information is
coming from.</p>
</div>
<div class="section" id="s-field-name-hiding-is-not-permitted">
<span id="field-name-hiding-is-not-permitted"></span><h3>Field name "hiding" is not permitted<a class="headerlink" href="#field-name-hiding-is-not-permitted" title="Permalink to this headline">¶</a></h3>
<p>In normal Python class inheritance, it is permissible for a child class to
override any attribute from the parent class. In Django, this is not permitted
for attributes that are <tt class="xref docutils literal"><span class="pre">Field</span></tt> instances (at
least, not at the moment). If a base class has a field called <tt class="docutils literal"><span class="pre">author</span></tt>, you
cannot create another model field called <tt class="docutils literal"><span class="pre">author</span></tt> in any class that inherits
from that base class.</p>
<p>Overriding fields in a parent model leads to difficulties in areas such as
initialising new instances (specifying which field is being intialised in
<tt class="docutils literal"><span class="pre">Model.__init__</span></tt>) and serialization. These are features which normal Python
class inheritance doesn't have to deal with in quite the same way, so the
difference between Django model inheritance and Python class inheritance isn't
merely arbitrary.</p>
<p>This restriction only applies to attributes which are
<tt class="xref docutils literal"><span class="pre">Field</span></tt> instances. Normal Python attributes
can be overridden if you wish. It also only applies to the name of the
attribute as Python sees it: if you are manually specifying the database
column name, you can have the same column name appearing in both a child and
an ancestor model for multi-table inheritance (they are columns in two
different database tables).</p>
<p>Django will raise a <tt class="docutils literal"><span class="pre">FieldError</span></tt> exception if you override any model field
in any ancestor model.</p>
</div>
</div>
</div>



<div id="content-secondary">
  <h2 id="comments">Questions/Feedback</h2>
  <p>Having trouble? We'd like to help!</p>
  <ul>
    <li>
      Try the <a href="http://docs.djangoproject.com/en/dev/faq/">FAQ</a> — it's got answers to many common
      questions.
    </li>
    <li>
      Search for information in the <a href="http://groups.google.com/group/django-users/">archives of the
      django-users mailing list</a>, or <a href="http://groups.google.com/group/django-users/">post a question</a>.
    </li>
    <li>
      Ask a question in the <a href="irc://irc.freenode.net/">#django IRC
      channel</a>, or search the <a href="http://oebfare.com/logger/django/">IRC
      logs</a> to see if its been asked before.
    </li>
    <li>
      If you notice errors with this documentation, please <a href="http://code.djangoproject.com/simpleticket?component=Documentation">
      open a ticket</a> and let us know! Please only use the ticket tracker for
      criticisms and improvements on the docs. For tech support, use the
      resources above.
    </li>
  </ul>
</div>

		</div>
		<!-- END #content-main -->
		<div id="content-related" class="sidebar">
		
  
    <h2>Contents</h2>
    
      <ul>
<li><a class="reference external" href="">Models</a><ul>
<li><a class="reference external" href="#quick-example">Quick example</a></li>
<li><a class="reference external" href="#using-models">Using models</a></li>
<li><a class="reference external" href="#fields">Fields</a><ul>
<li><a class="reference external" href="#field-types">Field types</a></li>
<li><a class="reference external" href="#field-options">Field options</a></li>
<li><a class="reference external" href="#id1">Automatic primary key fields</a></li>
<li><a class="reference external" href="#id2">Verbose field names</a></li>
<li><a class="reference external" href="#relationships">Relationships</a><ul>
<li><a class="reference external" href="#many-to-one-relationships">Many-to-one relationships</a></li>
<li><a class="reference external" href="#many-to-many-relationships">Many-to-many relationships</a></li>
<li><a class="reference external" href="#extra-fields-on-many-to-many-relationships">Extra fields on many-to-many relationships</a></li>
<li><a class="reference external" href="#one-to-one-relationships">One-to-one relationships</a></li>
</ul>
</li>
<li><a class="reference external" href="#models-across-files">Models across files</a></li>
<li><a class="reference external" href="#field-name-restrictions">Field name restrictions</a></li>
<li><a class="reference external" href="#custom-field-types">Custom field types</a></li>
</ul>
</li>
<li><a class="reference external" href="#id3">Meta options</a></li>
<li><a class="reference external" href="#id4">Model methods</a><ul>
<li><a class="reference external" href="#overriding-predefined-model-methods">Overriding predefined model methods</a></li>
<li><a class="reference external" href="#executing-custom-sql">Executing custom SQL</a></li>
</ul>
</li>
<li><a class="reference external" href="#id5">Model inheritance</a><ul>
<li><a class="reference external" href="#id6">Abstract base classes</a><ul>
<li><a class="reference external" href="#meta-inheritance"><tt class="docutils literal"><span class="pre">Meta</span></tt> inheritance</a></li>
<li><a class="reference external" href="#be-careful-with-related-name">Be careful with <tt class="docutils literal"><span class="pre">related_name</span></tt></a></li>
</ul>
</li>
<li><a class="reference external" href="#id7">Multi-table inheritance</a><ul>
<li><a class="reference external" href="#meta-and-multi-table-inheritance"><tt class="docutils literal"><span class="pre">Meta</span></tt> and multi-table inheritance</a></li>
<li><a class="reference external" href="#inheritance-and-reverse-relations">Inheritance and reverse relations</a></li>
<li><a class="reference external" href="#specifying-the-parent-link-field">Specifying the parent link field</a></li>
</ul>
</li>
<li><a class="reference external" href="#multiple-inheritance">Multiple inheritance</a></li>
<li><a class="reference external" href="#field-name-hiding-is-not-permitted">Field name “hiding” is not permitted</a></li>
</ul>
</li>
</ul>
</li>
</ul>

    
  
  
  
    <h2>Search</h2>
    
    <form action="/en/1.0/search/" id="search" class="search">
  <div>
    <input name="cx" value="009763561546736975936:e88ek0eurf4" type="hidden">
    <input name="cof" value="FORID:11" type="hidden">
    <input name="ie" value="UTF-8" type="hidden">
    <input name="hl" value="" type="hidden">
    <input style="background: rgb(255, 255, 255) url(http://www.google.com/coop/intl/en/images/google_custom_search_watermark.gif) no-repeat scroll left center; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" id="id_search_q" class="query" name="q" type="text">
    <input name="sa" class="submit" value="Search" type="submit">
    <ul>
<li><label for="id_search_as_q_0"><input checked="checked" id="id_search_as_q_0" value="more:dev_docs" name="as_q" type="radio"> Latest</label></li>
<li><label for="id_search_as_q_1"><input id="id_search_as_q_1" value="more:1.0_docs" name="as_q" type="radio"> 1.0</label></li>
<li><label for="id_search_as_q_2"><input id="id_search_as_q_2" value="more:0.96_docs" name="as_q" type="radio"> 0.96</label></li>
<li><label for="id_search_as_q_3"><input id="id_search_as_q_3" value="more:all_docs" name="as_q" type="radio"> All</label></li>
</ul>
  </div>
</form>
<script type="text/javascript" src="Django%20%7C%20Models%20%7C%20Django%20Documentation_files/brand.html"></script>
  
  
  
    <h2>Browse</h2>
    <ul>
      
        
          <li>Prev: <a href="http://docs.djangoproject.com/en/1.0/topics/db/">Models and databases</a></li>
        
        
          <li>Next: <a href="http://docs.djangoproject.com/en/1.0/topics/db/queries/">Making queries</a></li>
        
        <li><a href="http://docs.djangoproject.com/en/1.0/contents/">Table of contents</a></li>
        
          <li><a href="http://docs.djangoproject.com/en/1.0/genindex/">General Index</a></li>
        
          <li><a href="http://docs.djangoproject.com/en/1.0/modindex/">Global Module Index</a></li>
        
      
    </ul>
  
  
  
    <h2>You are here:</h2>
    <ul>
      
        <li>
          <a href="http://docs.djangoproject.com/en/1.0/">Django 1.0 documentation</a>
          
            <ul><li><a href="http://docs.djangoproject.com/en/1.0/topics/">Using Django</a>
          
            <ul><li><a href="http://docs.djangoproject.com/en/1.0/topics/db/">Models and databases</a>
          
          <ul><li>Models</li></ul>
          </li></ul></li></ul>
        </li>
      
    </ul>
  
  
  
    <h3>Last update:</h3>
    <div>May 23, 2009, 8:15 a.m. (<a href="http://www.timeanddate.com/worldclock/city.html?n=64">CDT</a>)</div>
  

		</div>
		<!-- END #content-related -->

		</div>
		<!-- END #content -->
		<div id="footer">
			<p>© 2005-2009 <a href="http://www.djangoproject.com/foundation/">Django Software Foundation</a> unless otherwise noted. Django is a registered trademark of the Django Software Foundation. 
			Hosting graciously provided by <a href="http://mediatemple.net/">
			<img style="vertical-align: middle; position: relative; top: -1px;" src="Django%20%7C%20Models%20%7C%20Django%20Documentation_files/mt.png" alt="media temple"></a>
			</p>
		</div>
		<!-- END #footer -->
	</div>
	<!-- END #container -->
	</body></html>